//
//  MM_HomeLeftMaskView.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/9/21.
//

#import "MM_HomeLeftMaskView.h"

@interface MM_HomeLeftMaskView ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *leftView;
@property (nonatomic, strong) UIImageView *userImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UIButton *updateBtn;
@property (nonatomic, strong) UIButton *exitBtn;

@end

@implementation MM_HomeLeftMaskView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5];
        
        _leftView = [[UIView alloc] init];
        _leftView.backgroundColor = MM_ColorUtil(@"#ffffff");
        [self addSubview:_leftView];
        
        _userImageView = [[UIImageView alloc] init];
        _userImageView.image = [UIImage imageNamed:@"depart_defaultHeader"];
        _userImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap_1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageClick)];
//        tap_1.delegate = self;
        tap_1.numberOfTapsRequired = 3;
        [_userImageView addGestureRecognizer:tap_1];
        [self.leftView addSubview:_userImageView];
        
        _nameLabel = [Custom_ZHTool uiLabelCreate:@"张先生" Font:FONT_BOLD(27) TextColor:MM_ColorUtil(@"#333333")];
        _nameLabel.userInteractionEnabled = YES;
        _nameLabel.text = [MM_Cache shareMM_Cache].userModel.user_name;
        [self.leftView addSubview:_nameLabel];
        
        _descLabel = [Custom_ZHTool uiLabelCreate:@"当前APP版本号:1.1.9" Font:FONT(18) TextColor:MM_ColorUtil(@"#333333")];
        _descLabel.userInteractionEnabled = YES;
        _descLabel.textAlignment = NSTextAlignmentCenter;
        [self.leftView addSubview:_descLabel];
        
        _updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_updateBtn MM_ConfigButtonWithTitle:@"版本更新" titleFont:FONT_BOLD(14) titleColor:MM_ColorUtil(@"#ffffff") bgColor:MM_ColorUtil(@"#1F71C4") cornerRadius:22 borderColor:nil borderWidth:0];
        _updateBtn.tag = 100;
        [_updateBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.leftView addSubview:_updateBtn];
        
        _exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_exitBtn MM_ConfigButtonWithTitle:@"退出登录" titleFont:FONT_BOLD(14) titleColor:MM_ColorUtil(@"#ffffff") bgColor:MM_ColorUtil(@"#1F71C4") cornerRadius:22 borderColor:nil borderWidth:0];
        _exitBtn.tag = 101;
        [_exitBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.leftView addSubview:_exitBtn];
                
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
//        tap.cancelsTouchesInView = NO;
        [self addGestureRecognizer:tap];

        [tap requireGestureRecognizerToFail:tap_1];

        [self setUpViews];
    }
    return self;
}

- (void)setUpViews {
    
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.top.bottom.equalTo(self);
        make.width.equalTo(250);
    }];
    
    [self.userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.leftView);
        make.top.equalTo(100);
        make.width.height.equalTo(75);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.leftView);
        make.top.equalTo(self.userImageView.mas_bottom).offset(10);
    }];
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.updateBtn).offset(-64);
        make.centerX.equalTo(self.leftView);
        make.left.equalTo(12);
        make.right.equalTo(-12);
    }];

    [self.updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.exitBtn).offset(-64);
        make.centerX.equalTo(self.leftView);
        make.width.equalTo(180);
        make.height.equalTo(44);
    }];
    
    [self.exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(self.leftView).offset(-50);
        make.centerX.equalTo(self.leftView);
        make.width.equalTo(180);
        make.height.equalTo(44);
    }];
}

- (void)tapImageClick {
    
    if (self.leftMaskBlock) {
        self.leftMaskBlock(2);
    }
}

- (void)btnClick:(UIButton *)button {
    
    NSInteger index = button.tag - 100;
    
    if (self.leftMaskBlock) {
        self.leftMaskBlock(index);
    }
}

- (void)tapClick:(UITapGestureRecognizer *)tap {
    
    if (self.leftMaskBlock) {
        self.leftMaskBlock(3);
    }
}

@end
